﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CreditCardApp
{
   public  class CreditCardApplicationEvaluator
    {
        private const int AutoReferMaxAge = 20;
        private const int HightIncomeThreshold = 100_000;
        private const int LowIncomeThreshold = 20_000;
        private readonly IFrequentFlyerNumberValidator _validator;
        private readonly FraudLookup _fraudLookup;

        public int ValidatorLookupCount { get; private set; }

        public CreditCardApplicationEvaluator(IFrequentFlyerNumberValidator validator, FraudLookup fraudLookup = null)
        {
            _validator = validator ?? throw new ArgumentNullException(nameof(validator));
            _validator.ValidatorLookupPerformed += ValidateLookupPerformed;
            _fraudLookup = fraudLookup;
        }

        private void ValidateLookupPerformed(object sender, EventArgs e)
        {
            ValidatorLookupCount++;
        }

        public CreditCardApplicationDecision Evaluate(CreditCardApplication application)
        {
            if(_fraudLookup!=null && _fraudLookup.IsFraudRisk(application))
            {
                return CreditCardApplicationDecision.ReferredToHumanFraudRisk;
            }


            if(application.GrossAnuualIncome >= HightIncomeThreshold)
            {
                return CreditCardApplicationDecision.AutoAccepted;
            }

            //if(_validator.LicenseKey=="EXPIRED")
            //{
            //    return CreditCardApplicationDecision.ReferedToHuman;
            //}


            if(_validator.ServiceInformation.License.LicenseKey == "EXPIRED")
            {
                return CreditCardApplicationDecision.ReferedToHuman;
            }


            _validator.ValidationMode = application.Age >= 30 ? ValidationMode.Detailed : ValidationMode.Quick;

            bool isValidFrequentFlyerNumber;
            try
            {
                 isValidFrequentFlyerNumber = _validator.IsValid(application.FrequentFlyerNumber);
            }
            catch (Exception)
            {

                //lot
                return CreditCardApplicationDecision.ReferedToHuman;
            }
            



            if(!isValidFrequentFlyerNumber)
            {
                return CreditCardApplicationDecision.ReferedToHuman;
            }

            if(application.Age <= AutoReferMaxAge)
            {
                return CreditCardApplicationDecision.ReferedToHuman;
            }

            if(application.GrossAnuualIncome < LowIncomeThreshold)
            {
                return CreditCardApplicationDecision.AutoDeclined;
            }

            return CreditCardApplicationDecision.ReferedToHuman;
        }


        //public CreditCardApplicationDecision EvaluateUsingOut(CreditCardApplication application)
        //{
        //    if(application.GrossAnuualIncome >= HightIncomeThreshold)
        //    {
        //        return CreditCardApplicationDecision.AutoAccepted;
        //    }

        //    _validator.IsValid(application.FrequentFlyerNumber, out var isValidFrequentFlyerNumber);

        //    if(!isValidFrequentFlyerNumber)
        //    {
        //        return CreditCardApplicationDecision.ReferedToHuman;
        //    }

        //    if(application.Age<=AutoReferMaxAge)
        //    {
        //        return CreditCardApplicationDecision.ReferedToHuman;
        //    }

        //    if (application.GrossAnuualIncome < LowIncomeThreshold)
        //    {
        //        return CreditCardApplicationDecision.AutoDeclined;
        //    }

        //    return CreditCardApplicationDecision.ReferedToHuman;
        //}
    }
}
